package com.lagou.edu.utils;


import java.sql.Connection;
import java.sql.SQLException;

/**
 * 线程绑定工具类
 * @author oyz
 */
public class ConnectionUtils {

    /*private ConnectionUtils() {

    }
    private static ConnectionUtils connectionUtils = new ConnectionUtils();

    public static ConnectionUtils getInstance(){
        return connectionUtils;
    }*/

    /**
     * 存储当前线程的连接
     */
    private ThreadLocal<Connection> threadLocal = new ThreadLocal<>();

    /**
     * 从当前线程获取连接
     */
    public Connection getCurrentThreadConn() throws SQLException {
        Connection connection = threadLocal.get();
        if(connection==null){
            connection = DruidUtils.getInstance().getConnection();
            threadLocal.set(connection);
        }
        return connection;
    }


}
